package com.erp.ae.controller;

import cn.hutool.core.lang.Dict;
import com.erp.ae.service.IAePromotionActivityService;
import com.ruoyi.common.core.domain.R;
import com.erp.ae.utils.AeApiUtils;
import com.ruoyi.common.core.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/promotionActivity")
public class AePromotionActivityController {

    private final IAePromotionActivityService iAePromotionActivityService;

    /**
     * 修改店铺活动
     */
    @PostMapping("/editActivityToAe")
    public R<Object> editActivityToAe(@RequestBody HashMap<String,Object> map) {

        Map discInputDto = new HashMap();
        discInputDto.put("promotion_end_time", map.get("promotion_end_time"));
        discInputDto.put("promotion_id", map.get("promotion_id"));
        discInputDto.put("promotion_name", map.get("promotion_name"));
        discInputDto.put("promotion_start_time", map.get("promotion_start_time"));
        String storeId = (String)map.get("storeId");
        return  iAePromotionActivityService.editActivityToAe(discInputDto,storeId);


    }


    /**
     * 发布活动至速卖通
     */
    @PostMapping("/createActivityToAe")
    public R<Object> createActivityToAe(@RequestBody HashMap<String,Object> map) {
        String storeId = (String)map.get("storeId");
        Map discInputDto = new HashMap();
        discInputDto.put("promotion_end_time", map.get("promotion_end_time"));
        discInputDto.put("has_promo", map.get("has_promo"));
        discInputDto.put("promotion_name", map.get("promotion_name"));
        discInputDto.put("promotion_start_time", map.get("promotion_start_time"));

        return  iAePromotionActivityService.createActivityToAe(discInputDto,storeId);


    }

    /**
     * 获取该商品在哪个有效的店铺活动中
     */
    @PostMapping("/getActivityByProduct")
    public R<Object> getActivityByProduct(@RequestBody Map<String, Object> map) {
        String storeId = (String) map.get("storeId");
        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("product_id", map.get("product_id"));

        return  iAePromotionActivityService.getActivityByProduct(storeId,msgMap);
    }



    /**
     * 同步店铺营销活动列表
     */
    @PostMapping("/selectStorePromotions")
    public R<Object> selectStorePromotions(@RequestBody Map<String, Object> map) {
        String storeId = (String) map.get("storeId");
        //生成参数Map
        //获取店铺的accessToken
        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("promotion_type", map.get("promotion_type"));
        msgMap.put("current_page", map.get("current_page"));
        msgMap.put("page_size", map.get("page_size"));
        return iAePromotionActivityService.selectStorePromotions(storeId,msgMap);
    }


    @GetMapping("/findSellerCouponActivity")
    public R<Object> findSellerCouponActivity(String activityId, String storeId) {
        return iAePromotionActivityService.findSellerCouponActivity(activityId,storeId);
    }

    /**
     * 将商品添加入活动
     */
    @PostMapping("/addProductToActivity")
    public R<Object> addProductToActivity(@RequestBody String productDtoStr) {
        System.out.println(productDtoStr);
        Dict dict = JsonUtils.parseMap(productDtoStr);
        Map<String, Object> map = new HashMap<>();
        map.put("promotion_id", dict.get("promotion_id"));
        map.put("exist_store_fans_discount", dict.get("exist_store_fans_discount"));
        map.put("product_objects", dict.get("product_objects"));
        String storeId = (String) dict.get("storeId");
        return iAePromotionActivityService.addProductToActivity(storeId,map);
    }

    /**
     * 修改商品折扣
     */
    @PostMapping("/updateProductInActivity")
    public R<Object> updateProductInActivity(@RequestBody String productDtoStr) {
        System.out.println(productDtoStr);
        Dict dict = JsonUtils.parseMap(productDtoStr);
        String storeId = (String) dict.get("storeId");
        Map<String, Object> map = new HashMap<>();
        map.put("promotion_id", dict.get("promotion_id"));
        map.put("exist_store_fans_discount", dict.get("exist_store_fans_discount"));
        map.put("store_club_discount_rate", dict.get("store_club_discount_rate"));
        map.put("product_objects", dict.get("product_objects"));
        return iAePromotionActivityService.updateProductInActivity(map,storeId);

    }

    /**
     * 在活动中删除商品
     */
    @PostMapping("/deleteProductInActivity")
    public R<Object> deleteProductInActivity(@RequestBody Map<String,Object> map) {
       return  iAePromotionActivityService.deleteProductInActivity(map);
    }

    /**
     * 更新活动列表
     * @param currentPage
     * @param storeId
     * @return
     */
    @GetMapping("/selectStoreOngoingPromotions/{currentPage}/{storeId}")
    public R<Object> selectStoreOngoingPromotions(
            @PathVariable("currentPage") String currentPage,
            @PathVariable("storeId") String storeId
    ) {
        return iAePromotionActivityService.selectStoreOngoingPromotions(currentPage, storeId);
    }

    @GetMapping("/queryStoreOngoingPromotionsMsg/{storeUserName}")
    public R<Object> queryStoreOngoingPromotionsMsg(
            @PathVariable("storeUserName") String storeUserName
    ) {
        return iAePromotionActivityService.queryStoreOngoingPromotionsMsg(storeUserName);
    }
}
